package com.itboye.eweilisten.responsitory;

import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.interfaces.TiMuInterface;
import com.itboye.eweilisten.me.personal.model.PersonDataBean;
import com.itboye.eweilisten.model.LianXiBean;
import com.itboye.eweilisten.model.TiXingBean;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.sputil.SPUtils;
import com.itboye.eweilisten.volley.BaseErrorListener;
import com.itboye.eweilisten.volley.BaseNetRepository;
import com.itboye.eweilisten.volley.BaseSuccessReqListener;
import com.itboye.eweilisten.volley.ByJsonRequest;
import com.itboye.eweilisten.volley.ICompleteListener;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * 做题
 */

public class TiMuResponsitory extends BaseNetRepository implements TiMuInterface<PersonDataBean> {
    private String api = "101";//接口版本号
    private String timu = "By_Question_test";// 题目
    private String xiangqing = "By_Question_unitDetail";// 题目详情
    private String tijiao = "By_Question_submitNoAnswer";// 提交问题

    public TiMuResponsitory(ICompleteListener iCompleteListener) {
        super(iCompleteListener);
    }
    @Override
    public void onTiMu(String dt_type) {
        Type type = new TypeToken<LianXiBean>() {
        }.getType();
        String apiVer = api;
        Map<String, Object> map = new HashMap<>();
        map.put("dt_type", dt_type);
        (new ByJsonRequest.Builder<LianXiBean>())
                .setTypeVerParamsAndReturnClass(timu, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<LianXiBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 题目详情
     * @param uid
     * @param unit_id
     * @param s_id
     */
    @Override
    public void onXiangQing(String uid, String unit_id, String s_id) {
        Type type = new TypeToken<TiXingBean>() {
        }.getType();
        String apiVer = api;
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("unit_id", unit_id);
        map.put("s_id", s_id);
        (new ByJsonRequest.Builder<TiXingBean>())
                .setTypeVerParamsAndReturnClass(xiangqing, "101", map, type)
                .requestListener(
                        new BaseSuccessReqListener<TiXingBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 提交答案
     * @param uid
     * @param unit_id
     * @param use_time
     * @param answer_multi
     */
    @Override
    public void onTiJiao(String uid, String unit_id, String use_time, String answer_multi) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = api;
        Map<String, Object> map = new HashMap<>();
        String  sid = (String) SPUtils.get(MyApplication.instance, null, Const.S_ID, "");
        map.put("uid", uid);
        map.put("unit_id", unit_id);
        map.put("s_id", sid);
        map.put("use_time", use_time);
        map.put("answer_multi", answer_multi);
        Log.d("============",uid+"=="+unit_id+"=="+sid+"=="+use_time+"=="+answer_multi);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(tijiao, "100", map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }
}
